using UnityEngine;
using System.Collections;

public class Pan2D: MonoBehaviour {
	
	static public float xMin = -163;
	static public float xMax = 163;
	
	public bool volumeScaling = true;
	public float baseVolume;
	
	private float[] mVolumes;

	// Use this for initialization
	void Start () 
	{
		/*if(volumeScaling)
		{
			AudioSource[] lSrcs = GetComponents<AudioSource>();
			mVolumes = new float[0];
			
			//CreateDefaultVolume();
		}*/
	}
	
	// Update is called once per frame
	void Update () 
	{
		AudioSource[] lSrcs = GetComponents<AudioSource>();
		float lValue = transform.position.x / 163.0f;
		float lVol = (163 - Mathf.Min(Mathf.Abs(transform.position.x), 163.0f))/163.0f;
		
		for(int i = 0; i < lSrcs.Length; i++)
		{
			lSrcs[i].pan = lValue;
			
			/*if(volumeScaling)
			{
				if(i >= mVolumes.Length)
				{
					CreateDefaultVolume();
				}
				
				lSrcs[i].volume = lVol * mVolumes[i];
			}*/
			
			if(volumeScaling)
				lSrcs[i].volume = lVol * baseVolume;
		}
	}
				                          
				                           
	void CreateDefaultVolume()
   	{
		AudioSource[] lSrcs = GetComponents<AudioSource>();
		float[] lTemps = new float[lSrcs.Length];
		
		int i;
		
		for(i = 0; i < mVolumes.Length; i++)
		{
			lTemps[i] = mVolumes[i];
		}
		
		for(int j = i; j < lSrcs.Length; j++)
		{
			lTemps[j] = lSrcs[i].volume;
		}
		
		mVolumes = new float[lSrcs.Length];
		for(int k = 0; k < lTemps.Length; k++)
		{
			mVolumes[k] = lTemps[k];
		}
   	}
}
